package edu.wpi.cs509.clientlogic;

import edu.wpi.cs509.network.Connection;
import edu.wpi.cs509.network.ConnectionOwner;
import edu.wpi.cs509.ui.intfc.ClientLogicNotify;

/**
 * The UserSession class is used by the Client Logic to track a unique
 * user session.  The initial implementation will only allow a single
 * active game, so only one UserSession should exist.
 * 
 */
public class UserSession implements ConnectionOwner {
	/** The network connection to the server. */
	private Connection serverConnection;
	private String username;
	
	public UserSession() {}
	
	public UserSession (Connection conn) {
		setConnection(conn);
	}
	
	public UserSession (String username) {
		setUsername(username);
	}
	
	/**
	 * sessionEvent is the client-notification logic.
	 */
	ClientLogicNotify sessionEvent;
	
	/**
	 * Constructor
	 * * @param sessionEvent - the event handler interface for session events
	 */
	public UserSession(ClientLogicNotify sessionEvent) {
		this.sessionEvent = sessionEvent;
	}
	
	/**
	 * @return The network connection to the server.
	 */
	public Connection getConnection() {
		return serverConnection;
	}
	
	/**
	 * @param serverConnection The network connection to the server.
	 */
	public void setConnection(Connection serverConnection) {
		this.serverConnection = serverConnection;
	}

	/**
	 * @return Client's username.
	 */
	public String getUsername() {
		return username;
	}
	
	/**
	 * @param username The username of the user.
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	
	/**
	 * Called when the network connection is terminated.
	 */
	@Override
	public void handleDisconnect(Connection connection) {
		//Set the ClientLogic pointer to ourselves to null so we know our sessions is dead.
		System.out.println("Handling disconnect of user session");
		ClientLogic.getInstance().setUserSession(null);
		ClientUpstreamLogic.getInstance().receiveServerConnectionLost();
	}
	
}
